with tab as (
 select billcode,
       split(concat(scantime_early,'\001',
       coalesce(inputtime_early,''),'\001',
       coalesce(nextstationcode_early,''),'\001',
       coalesce(packagecode_early,''),'\001',
       coalesce(scanuser_early,''),'\001',
       coalesce(weight_early,''),'\001',
       '快件揽收','\001',
       coalesce(scansitecode_early,'')
       ),'\001') as wid_columns
from jms_dwd.dwd_barscan_taking_dt      --21天内有效
where  dt BETWEEN date_add('{{ execution_date | cst_ds }}', -24) AND '{{ execution_date | cst_ds }}'
    union all
 select billcode,
       split(concat(scantime_early,'\001',
       coalesce(inputtime_early,''),'\001',
       coalesce(nextstationcode_early,''),'\001',
       coalesce(packagecode_early,''),'\001',
       coalesce(scanuser_early,''),'\001',
       coalesce(weight_early,''),'\001',
       '入仓扫描','\001',
       coalesce(scansitecode_early,'')
       ),'\001') as wid_columns
from jms_dwd.dwd_barscan_warehousing_dt      --21天内有效
where  dt BETWEEN date_add('{{ execution_date | cst_ds }}', -24) AND '{{ execution_date | cst_ds }}' and dimtype='billcode'
)
insert overwrite table jms_dws.dws_actual_taking_dt
select
billcode                                                 as billcode,
min(wid_columns)[7]                                      as scansitecode_early,
min(wid_columns)[0]                                      as scantime_early,
min(wid_columns)[1]                                      as inputtime_early,
min(wid_columns)[2]                                      as nextstationcode_early,
min(wid_columns)[3]                                      as packagecode_early,
min(wid_columns)[4]                                      as scanuser_early,
cast(min(wid_columns)[5] AS DECIMAL(10, 2))              as weight_early,
min(wid_columns)[6]                                      as scantype_early,
date(min(wid_columns)[0])                                as dt
from tab
 group by billcode
having
    date(min(wid_columns)[0]) between date_add('{{ execution_date | cst_ds }}', -14) AND '{{ execution_date | cst_ds }}'
distribute by dt , pmod(hash(rand()),18);
